﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using cart.dao;
using cart.model;
using System.Collections;

namespace cart.service
{
    public class cartService
    {
        public Boolean addCart(cartModel cm)
        {
            cartDao cd = new cartDao();

            ArrayList alcart = cd.getCartByUserNameOrProductID(cm.userName, cm.productID);
            if (0 == alcart.Count)
            {
                return cd.addCart(cm);
            }
            else
            {
                //cm.productNumber = cm.productNumber + 1;
                cm = (cartModel)alcart[0];
                cm.productNumber = cm.productNumber + 1;
                return cd.updateCartItem(cm);
            }
        }

        /// <summary>
        /// 删除购物车里的一条记录
        /// </summary>
        /// <param name="cm"></param>
        /// <returns></returns>
        public Boolean deleteCartItem(cartModel cm)
        {
            cartDao cs = new cartDao();
            return cs.deleteCartItem(cm);
        }

        public Boolean clearCart(String userID)
        {
            cartDao cd = new cartDao();
            return cd.clearCart(userID);
        }

        /// <summary>
        /// 更新购物车里的商品
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="productID"></param>
        /// <returns></returns>
        public Boolean updateCartItem(cartModel cm)
        {
            cartDao cd = new cartDao();
            return cd.updateCartItem(cm);
        }

        public ArrayList getCartByUserNameOrProductID(string userName)
        {
            cartDao cd = new cartDao();
            return cd.getCartByUserNameOrProductID(userName);
        }

        public ArrayList getCartByUserNameOrProductID(string userName,int productID)
        {
            cartDao cs = new cartDao();
            return cs.getCartByUserNameOrProductID(userName,productID);
        }


        public ArrayList getCart(ArrayList alCartItems)
        {
            ArrayList alOrderItemsList = new ArrayList();
            ProductModel pm = new ProductModel();
            productService ps = new productService();


            foreach (cartModel cm in alCartItems)
            {
                orderItemsModel oim = new orderItemsModel();
                pm = ps.getProdcutByID(cm.productID);

                oim.productID = pm.productID;
                oim.prodcutName = pm.name;
                oim.score = pm.score;
                oim.marketPrice = pm.marketPrice;
                oim.memberPrice = pm.memberPrice;
                oim.productNumber = cm.productNumber;

                alOrderItemsList.Add(oim);
            }

            return alOrderItemsList;
        }

        public double getCartTotle(ArrayList alCartItems)
        {
            //orderItemsModel oim = new orderItemsModel();
            orderItemsService ois = new orderItemsService();
            double totle = 0;
            foreach (orderItemsModel oim in alCartItems)
            {
                totle = totle + ois.getSubTotle(oim);
            }
            return totle;
        }
    }
}
